#! /usr/bin/env python

# Convert a PSRCHIVE archive to a binary blob

import psrchive

from optparse import OptionParser
usage = """Usage: %prog [options] file (file2 ...)

Converts data from PSRCHIVE archive files to a simple binary 
blob.  Data is stored in profile bin, channel, polarization, subint
order (fast to slow)."""
par = OptionParser(usage=usage)
par.add_option("-e", "--ext", dest="ext", help="Output file extension (.bin)",
        action="store", type="string", default="bin")
(opt,args) = par.parse_args()

for f in args:
    try:
        ar = psrchive.Archive_load(f)
        outf = f + "." + opt.ext
        print "Writing '%s'..." % outf
        ar.get_data().tofile(outf)
    except:
        print "Error processing '%s'" % f

